<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="放映厅" prop="hallId">
        <el-select
          clearable
          v-model="dataForm.hallId"
          @change="hallChange"
          placeholder="请选择放映厅"
        >
          <el-option
            v-for="item in hallList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电影" prop="movieId">
        <el-select
          clearable
          v-model="dataForm.movieId"
          @change="movieChange"
          placeholder="请选择电影"
        >
          <el-option
            v-for="item in movieList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="放映时间" prop="startTime">
        <!-- <el-date-picker
        @focus='dateFocus'
        value-format="yyyy-MM-dd HH:mm:ss"
          v-model="saeTime"
          @change="dateChange"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="saeTime"
          :picker-options='rangeTime'
        > -->
        <!-- </el-date-picker> -->
        <el-date-picker
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="dataForm.startTime"
          :picker-options="rangeTime"
          @blur="dateBlur"
          @focus="dateFocus"
          @change="dateChange"
          v-model="dataForm.startTime"
          type="datetime"
          placeholder="选择日期时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="价格(￥)" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格(￥)"></el-input>
      </el-form-item>
      <el-form-item label="上架状态" prop="status">
        <el-switch
          style="display: block"
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-text="激活"
          inactive-text="禁用"
          :active-value="0"
          :inactive-value="1"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      focus: false,
      movieRelease: "",
      movieEnd: "",
      disabledDate1: "",
      disabledDate2: "",
      disAll: false,
      saeTime: "",
      visible: false,
      movieList: [],
      hallList: [],
      dataForm: {
        id: 0,
        startTime: "",
        price: "",
        status: "",
        movieId: "",
        hallId: "",
        endTime: "",
      },
      rangeTime: {
        disabledDate: (time) => {
          if (this.dataForm.movieId && this.dataForm.hallId) {
            // console.log(new Date())
            // console.log(this.releaseDate)
            return (
              time.getTime() < new Date() ||
              time.getTime() < this.movieRelease ||
              this.movieEnd < time.getTime()
            );
          } else {
            return time.getTime();
          }
        },
      },
      dataRule: {
        startTime: [
          { required: true, message: "电影放映时间不能为空", trigger: "blur" },
        ],
        price: [{ required: true, message: "价格", trigger: "blur" }],
        status: [{ required: true, message: " 状态上架", trigger: "blur" }],
        choiceSeat: [
          { required: true, message: "选择座位不能为空", trigger: "blur" },
        ],
        movieId: [
          { required: true, message: "电影id不能为空", trigger: "blur" },
        ],
        hallId: [
          { required: true, message: "所属放映厅id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    dateBlur() {
      if (!this.focus) {
        return;
      }
      this.focus = false;
    },
    dateFocus() {
      if (this.focus) {
        return;
      }
      if (!this.dataForm.movieId || !this.dataForm.hallId) {
        this.$message.error("请选择电影跟放映厅");
        return;
      }
      this.$http({
        url: this.$http.adornUrl(`/cb/movie/info/${this.dataForm.movieId}`),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.movieRelease = new Date(data.movie.releaseDate);
          this.movieEnd = new Date(data.movie.endDate);
          console.log(this.movieRelease, this.movieEnd);
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    dateChange(row) {
      this.$http({
        url: this.$http.adornUrl(`/cb/schedule/check`),
        method: "post",
        data: this.$http.adornParams({
          startTime: row,
          endTime: "",
          movieId: this.dataForm.movieId,
          hallId: this.dataForm.hallId,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataForm.startTime = row;
        } else {
          this.$message.error(data.msg);

          this.dataForm.startTime = "";
        }
      });
    },
    movieChange(row) {
      console.log(row);
    },
    hallChange(row) {
      console.log(row);
    },
    init(id, movies, halls) {
      this.saeTime = [];
      this.movieList = movies;
      this.hallList = halls;
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        this.dataForm.movieId = "";
        this.dataForm.hallId = "";
        console.log(this.dataForm);
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/cb/schedule/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.startTime = data.schedule.startTime;
              this.dataForm.endTime = data.schedule.endTime;

              // if (this.dataForm.startTime) {
              //   this.saeTime.push(this.dataForm.startTime);
              // }
              // if (this.dataForm.endTime) {
              //   this.saeTime.push(this.dataForm.endTime);
              // }
              this.dataForm.price = data.schedule.price;
              this.dataForm.status = data.schedule.status;
              this.dataForm.movieId = data.schedule.movieId;
              this.dataForm.hallId = data.schedule.hallId;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/cb/schedule/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              startTime: this.dataForm.startTime,
              endTime: this.dataForm.endTime,
              price: this.dataForm.price,
              status: this.dataForm.status,
              movieId: this.dataForm.movieId,
              hallId: this.dataForm.hallId,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
